﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;
using System.Collections.Specialized;

namespace ThietBiSo.Controllers
{
    public class danhmucController : ApplicationController
    {
        //
        // GET: /LinhKien/
        MDanhMuc mdm = new MDanhMuc();
        DanhMuc dm = new DanhMuc();
        MDanhMucCon mdmc = new MDanhMucCon();
        DanhMucCon dmc = new DanhMucCon();

        MSanPham msp = new MSanPham();

        #region admin danh mục

        public ActionResult Index(int? id)
        {
           
            int pageString = id ?? 0;
            int page = pageString == 0 ? 1 : pageString;

          
            // GET : 
            int total = mdm.GetTotal();

            PagingController("/danhmuc/Index/", total, page, "");
          
            // End paging
            List<DanhMuc> ldm = new List<DanhMuc>();
            ldm = mdm.GetList(start, offset);

            //RESPONSE  DATA
            ViewData["danhmucs"] = ldm;
           

            return View();
        }
      

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: 

        public ActionResult Create()
        {
            //string urlcbNhom = Request.QueryString["cbNhom"];
            //ViewData["urlcbNhom"] = urlcbNhom;
            return View();
        } 

        //
        // POST: 

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
               // string urlcbNhom = collection.Get("cbNhom");
                dm.MaDanhMuc = mdm.GetMax() + 1;
              
                dm.TenDanhMuc = collection.Get("TenDanhMuc");
                dm.HienThi = true;
                if (mdm.GetMaxTT() == 0 || mdm.GetTotal() == 0)
                {
                    dm.ThuTu = 1;
                }
                else
                    dm.ThuTu = mdm.GetMaxTT() + 1;
                mdm.Save(dm);
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /LinhKien/Edit/5
 
        public ActionResult Edit(int id)
        {
            ViewData["danhmucs"] = mdm.GetByID(id);
            //string urlcbNhom = Request.QueryString["cbNhom"];
            //ViewData["urlcbNhom"] = urlcbNhom;

            return View();
        }

        //
        // POST: /LinhKien/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
               // string urlcbNhom = collection.Get("cbNhom");
                dm.MaDanhMuc = id;
               
                dm.TenDanhMuc = collection.Get("TenDanhMuc");                
                dm.ThuTu = int.Parse(collection.Get("ThuTu"));
                mdm.Update(dm);
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }

        //
     

      


        public ActionResult danhmuccon()
        {
            string id = Request.QueryString["id"];
            ViewData["urlid"] = id;
            ViewData["danhmuccons"] = mdmc.FindAll(int.Parse(id));
            return View();
        }

        //
        // POST: /LinhKien/Create

        [HttpPost]
        public ActionResult danhmuccon(FormCollection collection)
        {
            string id = Request.QueryString["id"];

            dmc.MaDMCon = mdmc.GetMax() + 1;
            dmc.MaDanhMuc = int.Parse(collection.Get("MaDM"));
            dmc.TenDMCon = collection.Get("TenDMCon");
            dmc.HienThi = true;

            if (mdmc.GetTotal(int.Parse(collection.Get("MaDM"))) == 0)
            {
                dmc.ThuTu = 0;
            }
            else
                dmc.ThuTu = mdmc.GetMaxTT(int.Parse(collection.Get("MaDM"))) + 1;


            //isp.ThuTu = mis.GetMaxTT() + 1;
            mdmc.Save(dmc);
            NameValueCollection querystring = HttpUtility.ParseQueryString(Request.UrlReferrer.Query);
            string url = Request.UrlReferrer.AbsolutePath
                        + "?" + querystring.ToString();
            return Redirect(url);
        }

        
         public ActionResult EditDMCon(int id)
         {
            
             ViewData["danhmuccons"] = mdmc.GetByID(id);
             string urlcbNhom = Request.QueryString["cbNhom"];
             ViewData["urlcbNhom"] = urlcbNhom;
             return View();
         }

         //
         // POST: /Edit/5

         [HttpPost]
         public ActionResult EditDMCon(int id, FormCollection collection)
         {
             try
             {
                 // TODO: Add update logic here
                 dmc.MaDMCon = id;
               
                 dmc.MaDanhMuc = int.Parse(collection.Get("cbNhom"));
                 dmc.TenDMCon = collection.Get("TenDMCon");
                 dmc.ThuTu = int.Parse(collection.Get("ThuTu"));
                 mdmc.Update(dmc);
                 return Redirect("/danhmuc/danhmuccon?id=" + collection.Get("cbNhom"));
             }
             catch
             {
                 return View();
             }
         }

         [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult Delete(int ma)
         {

             string error = mdm.Delete(ma);
             if (!String.IsNullOrEmpty(error))
                 return Json(error);
             return Json(String.Empty);
         }

         [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult HienThi(int ma)
         {

             string error = mdm.HienThi(ma);
             if (!String.IsNullOrEmpty(error))
                 return Json(error);
             return Json(String.Empty);
         }

         [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult DeleteDMC(int ma)
         {

             string error = mdmc.Delete(ma);
             if (!String.IsNullOrEmpty(error))
                 return Json(error);
             return Json(String.Empty);
         }

         [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult HienThiDMC(int ma)
         {

             string error = mdmc.HienThi(ma);
             if (!String.IsNullOrEmpty(error))
                 return Json(error);
             return Json(String.Empty);
         }
        #endregion

         #region website

         public ActionResult web(int id)
         {
             ViewData["sanphams"] = msp.GetByID(id);
             return View();
         }

         #endregion


    }
}
